package cn.edu.lzzy.mypractices.web.model;

import cn.edu.lzzy.mypractices.entity.*;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

/**
 * @author lzzy on 2022/11/10.
 * Description:封装控制器返回的消息
 */
public class ApiResult {
    private final boolean success;
    private final String message;
    private final Object data;

    public ApiResult(boolean success, String message, Object data) {
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }

    /**
     * @author lzzy on 2021/11/19.
     * Description:
     */
    @Getter
    @Setter
    public static class VmCourse {
        private UUID id;
        private String name;
        private String desc;
        private String cover;
        private Date createTime;
        private Date updateTime;
        private boolean open;
        private UUID teacherId;
        private String teacherName;
        private String teacherAvatar;
        private int countStudents;
        private int countChapters;

        private VmCourse() {
        }

        public static VmCourse create(Course course, boolean teacher) {
            VmCourse vm = new VmCourse();
            vm.setId(course.getId());
            vm.setName(course.getName());
            vm.setDesc(course.getDescription());
            vm.setCover(course.getCover());
            vm.setCreateTime(course.getCreateTime());
            vm.setUpdateTime(course.getUpdateTime());
            vm.setOpen(course.isOpen());
            vm.setTeacherAvatar(course.getTeacher().getAvatar());
            vm.setTeacherName(course.getTeacher().getNickName());
            vm.setCountStudents(course.getStudents().size());
            if (teacher) {
                vm.setTeacherId(course.getTeacher().getId());
            }
            return vm;
        }
    }

    @Getter
    @Setter
    public static class VmChapter {
        private UUID id;
        private String name;
        private String desc;
        private Date createTime;
        private Date updateTime;
        private boolean open;

        public static VmChapter create(Chapter chapter) {
            VmChapter vc = new VmChapter();
            vc.setId(chapter.getId());
            vc.setName(chapter.getName());
            vc.setDesc(chapter.getDescription());
            vc.setCreateTime(chapter.getCreateTime());
            vc.setUpdateTime(chapter.getUpdateTime());
            vc.setOpen(chapter.isOpen());
            return vc;
        }
    }

    @Getter
    @Setter
    public static class VmQuestion {
        private UUID id;
        private String content;
        private String analysis;
        private int ordinal;
        private List<VmOption> options;

        public static VmQuestion create(Question question, boolean isCreate) {
            VmQuestion vq = new VmQuestion();
            vq.setId(question.getId());
            vq.setContent(question.getContent());
            vq.setAnalysis(question.getAnalysis());
            vq.setOrdinal(question.getOrdinal());

            if (!isCreate) {
                List<VmOption> o = new LinkedList<>();
                question.getOptions().forEach(q -> o.add(VmOption.create(q)));
                vq.setOptions(o);
            }

            return vq;
        }
    }

    @Getter
    @Setter
    public static class VmOption {
        private UUID id;
        private String content;
        private String label;
        private boolean isAnswer;

        public static VmOption create(Option option) {
            VmOption vo = new VmOption();
            vo.setId(option.getId());
            vo.setContent(option.getContent());
            vo.setLabel(option.getLabel());
            vo.setAnswer(option.isAnswer());
            return vo;
        }
    }

    @Getter
    @Setter
    public static class VmUser {
        private UUID id;
        private String username;
        private String nickname;

        public static VmUser create(User user) {
            VmUser vu = new VmUser();
            vu.setId(user.getId());
            vu.setUsername(user.getUserName());
            vu.setNickname(user.getNickName());
            return vu;
        }
    }
}
